var mysqlPool = require('../lib/db.js');
var config = require('../config.js');

var pageSize = config.listShow.pageSize; //页码
/**
 * [listShowByUploadTime 根据日期倒序排序分页显示]
 * @param  {[type]} lastUpload [日期]
 * @param  {[type]} pageNum    [页码]
 * @param  {[type]} callBack   [回调函数]
 */
exports.listShowByUploadTime = function(lastUpload,pageNum,callBack) {
	var listShow = 'select a.*,b.username,b.avatar from ( '
		+' select * from tb_show where uploadtime < ? order by uploadtime desc limit ?,? ) a left join '
        +' tb_user b on a.userid = b.id ';
	
	var index = (pageNum - 1) * pageSize;
	
	mysqlPool.query(listShow,[lastUpload,index,pageSize],function(err, rows, fields) {
		if (err) callBack(err);
		callBack(null,rows);
	});
};


/**
 * [hasMoreShow 判断是否还有更多加载]
 * @param  {[type]} lastUpload [日期]
 * @param  {[type]} pageNum    [页码]
 * @param  {[type]} callBack   [回调函数]
 * @return {Boolean}            [description]
 */
exports.hasMoreShow = function(post,callBack) {
	var countShow = 'select count(id) as count from (select id from tb_show where uploadtime < ? order by uploadtime desc limit ?,?) a';
	var index = (pageNum - 1) * pageSize;
	mysqlPool.query(countShow,[lastUpload,index,pageSize],function(err, rows, fields) {
		if (err) callBack(err);
		callBack(null,rows);
	});	
};



exports.addShow = function(post,callBack) {
	var addShow = 'insert into tb_show(userid,uploadtime,uploadimg,remark,clike,ccomment) values '
			+' (?,now(),?,?,1,0)';
	
	var query = mysqlPool.query(addShow,[post.userid,post.uploadImg,post.remark], function(err, result) {
		if (err) callBack(err);
		callBack(null,result);
	});
};